package cn.lls.controller.ca;


import cn.lls.base.result.R;
import cn.lls.entity.sys.PermissionEntity;
import cn.lls.service.IPermissionService;
import cn.lls.vo.permission.PermissionCheckBoxVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  权限控制器
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */

@RestController
@RequestMapping("/ca/permission")
@Slf4j
@Api(tags = "管理员-权限控制器")
@CrossOrigin(origins = "*")
public class CAPermissionController {

    @Autowired
    private IPermissionService permissionService;


    @ApiOperation(value = "获取所有权限(checkbox使用)", response = R.class, httpMethod = "GET")
    @GetMapping("/checkbox")
    public R getPermissionSForCheckBox(){
        LambdaQueryWrapper<PermissionEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 此处 checkbox 只给公司管理员为公司职工分配权限使用，因此 apply = 2
        queryWrapper.eq(PermissionEntity::getApply, 2);
        queryWrapper.eq(PermissionEntity::getParentId, 0);
        List<PermissionEntity> list = permissionService.list(queryWrapper);
        return R.success(list.stream().map((item)->{
            PermissionCheckBoxVO permissionCheckBoxVO = new PermissionCheckBoxVO();
            BeanUtils.copyProperties(item, permissionCheckBoxVO);
            return permissionCheckBoxVO;
        }).collect(Collectors.toList()));
    }

}

